/*******************************************************************************
*   				               DOFILE: REGRESSION TABLES					                       *
********************************************************************************

    **  OUTLINE:

        - All regression tables

    **  NOTES:

        - Tables are generated in csv format and need to be imported into excel
        - The data on which the regressions and multiple hypotheses test corrections
          are run is a reshaped version of the main constructed data file.
          The reason for this is that the rwolf command (or any other mht
          correction command) does not allow for regressions with different
          controls. To overcome this issue all control variables are always included
          but are zero (and thus ommitted) when the control variable does not apply.

********************************************************************************/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  * Load dataset
  use "$Endline_dtFin/data_angolacbm_MHT.dta", clear

  * Set number of repetitions for mulitple hypotheses testing
  local reps 100

  // Specify dependent variables in each table

    // Table 3: Parents mobilization

    global table3 parentmob1_z parentmob2_z parentmobT_z formalparti_z

    global table3_individual  ///
      P_homework P_dinner P_rules P_sleep ///
      P_talk_teacher P_info ///
      T_parents_involvement T_problem16 T_parents_availablity p_inmeeting T_perf_comm ///
      S_comm S_comm_members S_board_parent_rep P_parti

    // Table 4: Treatment effects on the behavioral experiment measures

    global table4 Gp_dgf0_2 Gp_tg Gt_dg_r1 Gt_tg

    // Table 5: School management

    global table5 parentsonM_z onF_z publicinfo_z safety_z higiene_z material_z

    global table5_individual ///
      P_director_respons P_problem17 P_school_good ///
      S_info1 S_info2 S_info3 S_info4 S_info5 S_info6 S_info7 S_info8 ///
      P_problem4 P_problem6 P_problem7 P_problem8 P_problem28 P_problem29 P_problem30 P_problem31 P_problem32 ///
      D_problem4 D_problem6 D_problem7 D_problem8 D_problem28 D_problem29 D_problem30 D_problem31 D_problem32 ///
      T_problem4 T_problem6 T_problem7 T_problem8 T_problem28 T_problem29 T_problem30 T_problem31 T_problem32 ///
      I_classrooms_improvement I_security I_fence I_fence_impr_const ///
      I_cleaner bathroom_students A_water ///
      I_chair_desk_usable_sh S_new_exerbooks S_new_chalk

    // Table 6: Teacher performance

    global table6 pres_teacher parentonT_z directoronT_z
    *global table5 pres_teacher parentonT_z directoronT_z Gt_dg_r1 Gt_tg

    global table6_individual ///
      pres_teacher1 pres_teacher2 ///
      P_perf_teachers P_problem19 P_problem21 P_problem22 P_teacher_good  ///
      D_perf_teachers D_problem19 D_problem21 D_problem22 ///

    // Table 7: Student performance

    global table7 P_perf_student_z A_repeat_share pres_stud1 test_34_z

    global table7_individual ///
      test_pt3 test_mat3 test_pt4 test_mat4 ///

    // Define controls

    global sXb /// school controls
    teachersbaseline classroomsbaseline studentsbaseline

    global pX		/// parent controls
    P_age P_female P_primedu P_partner P_children P_student_age P_student_female ///
    P_parent P_student_grade_3 P_student_grade_4 P_student_grade_5 ///

    global tX		/// teacher controls
    T_age T_female T_highedu T_partner T_children T_teacher_preschool 	///
    T_teacher_grade_1 T_teacher_grade_2 T_teacher_grade_3 T_teacher_grade_4 ///
    T_teacher_grade_5 T_teacher_grade_6 T_teacher_grade_7 T_teacher_grade_8 ///
    T_teacher_grade_9  T_teacher_module1 T_teacher_module2 T_teacher_module3 T_exp_teach

    global dX		/// director controls
    D_age D_female D_highedu D_partner D_children

    global stX   /// student test controls
    test_grade test_type

    // Sets of controls

    global controls i.V $sXb $pX $tX $dX i.test_grade i.test_type

    global controlsT1 T2 T3 i.V $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT2 T1 T3 i.V $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT3 T1 T2 i.V $sXb $pX $tX $dX i.test_grade i.test_type

    global controls_block i.block i.V $sXb $pX $tX $dX i.test_grade i.test_type

    global controlsT1_block T2 T3 i.V i.block $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT2_block T1 T3 i.V i.block $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT3_block T1 T2 i.V i.block $sXb $pX $tX $dX i.test_grade i.test_type

    // Controls Table 8 for the rwolf estimation
    global controlsT1xtop_perf T1 T2 T3 T2xtop_perf T3xtop_perf top_perf i.V $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT2xtop_perf T1 T2 T3 T1xtop_perf T3xtop_perf top_perf i.V $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT3xtop_perf T1 T2 T3 T1xtop_perf T2xtop_perf top_perf i.V $sXb $pX $tX $dX i.test_grade i.test_type

    // Controls Table 9 for the rwolf estimation
    global controlsT1xlow_educ T1 T2 T3 T2xlow_educ T3xlow_educ low_educ i.V $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT2xlow_educ T1 T2 T3 T1xlow_educ T3xlow_educ low_educ i.V $sXb $pX $tX $dX i.test_grade i.test_type
    global controlsT3xlow_educ T1 T2 T3 T1xlow_educ T2xlow_educ low_educ i.V $sXb $pX $tX $dX i.test_grade i.test_type

    global controls_no i.V
    global controlsT1_no T2 T3 i.V
    global controlsT2_no T1 T3 i.V
    global controlsT3_no T1 T2 i.V

    global controls_block2 i.block
    global controlsT1_block2 T2 T3 i.block
    global controlsT2_block2 T1 T3 i.block
    global controlsT3_block2 T1 T2 i.block

    // Other

    global tables34567 $table3 $table4 $table5 $table6 $table7
    global tables3567_individual $table3_individual $table5_individual $table6_individual $table7_individual
    global table89 parentmob1_z formalparti_z test_3_z test_4_z

    global tableA11 $table3
    global tableA12 $table4
    global tableA13 $table5
    global tableA14 $table6
    global tableA15 $table7

    global tableA16 parentmob1_z P_homework_z P_dinner_z P_rules_z P_sleep_z
    global tableA17 parentmob1_z parentmob2_z parentmobT_z formalparti_rr_z

    // Table titles

    global title3 "Treatment effects on parents’ mobilization"
    global title4 "Treatment effects on the behavioral experiment measures"
    global title5 "Treatment effects on the quality of school management"
    global title6 "Treatment effects on teachers' performance"
    global title7 "Treatment effects on students' performance"

    global titleA11 "Treatment effects on parents’ mobilization (incl. block dummies)"
    global titleA12 "Treatment effects on the behavioral experiment measures (incl. block dummies)"
    global titleA13 "Treatment effects on the quality of school management (incl. block dummies)"
    global titleA14 "Treatment effects on teachers' performance (incl. block dummies)"
    global titleA15 "Treatment effects on students' performance (incl. block dummies)"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* 1. Main regression Tables

  * Tables 3 to 7

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// Calculate multiple hypotheses testing adjusted p-values
   * By row on each treatment

  // Romano-Wolf Multiple Hypothesis Correction for Tables 3-7


  forval x=3/7{
    forval i=1/3{
      rwolf ${table`x'}, ///
        indepvar(T`i') ///
        controls(${controlsT`i'}) ///
        vce(cluster school_id) ///
        cluster(school_id) ///
        reps(`reps') ///
        seed(20190723)

      foreach var in ${table`x'}{
        local `var'_scalar : di %-4.3f `e(rw_`var')'
        local `var'_rw`i' "[``var'_scalar']"
      }
    }
  }

  // Regressions Tables 3-7

  local tables3456 $tables34567

  foreach var in $tables34567{
    qui: reg `var' T1 T2 T3 $controls, robust cluster(school_id)
    eststo `var'
      // adding scalars:
      forval i=1/3{
        estadd local rwolf`i' ``var'_rw`i'', replace
      }
    local noobs_scalar = round(e(N))
    local noobs "`noobs_scalar'"
      estadd local noobs `noobs', replace
    qui: sum `var' if T0==1
      estadd scalar cmean = abs(r(mean))
    qui: test T1+T2=T3
      estadd scalar complement = r(p), replace
    qui: test T1 T2 T3
      estadd scalar joint_all = r(p), replace
    qui: test T1=T2
      estadd scalar inf_col = r(p), replace
    qui: test T1=T3
      estadd scalar inf_duo = r(p), replace
    qui: test T2=T3
      estadd scalar col_duo = r(p), replace
  }


  // Output Tables 3 to 7

  forval x=3/7{
    esttab ${table`x'} ///
      using "$Endline_outRaw/table`x'.csv", replace ///
      label b(3) se mtitle() ///
      keep(T1 T2 T3) s(rwolf1 rwolf2 rwolf3 cmean r2 noobs inf_col inf_duo col_duo complement joint_all, ///
      label("T1 RW P-value" "T2 RW P-value" "T3 RW P-value" ///
        "Mean dep. variable (control)" "R-squared" "Observations" ///
        "T1 = T2 (F p-val)" ///
        "T1 = T3 (F p-val)" ///
        "T2 = T3 (F p-val)" ///
        "T1 + T2 = T3 (F p-val)" ///
        "T1 = T2 = T3 = 0 (F p-val)" ///
      )) title("Table `x': ${title`x'}") ///
      starl(* 0.10 ** 0.05 *** 0.01) nonotes
  }


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* 2. Heterogeneous effects

* Heterogenous effects by baseline school performance and parents' education
* Tables 8 and 9

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


  // Table 8 (Baseline performance)

  // Calculate multiple hypotheses testing adjusted p-values
     * By row on the interaction term


    // Romano-Wolf Multiple Hypothesis Correction for Table 8

    forval i=1/3{
      rwolf $table89, ///
        indepvar(T`i'xtop_perf) ///
        controls(${controlsT`i'xtop_perf}) ///
        vce(cluster school_id) ///
        cluster(school_id) ///
        reps(`reps') ///
        seed(20190723)

      foreach var in $table89{
        local `var'_scalar : di %-4.3f `e(rw_`var')'
        local `var'_rw`i' "[``var'_scalar']"
      }
    }

  // Regressions Table 8

  foreach var in $table89{
    qui: reg `var' T1 T2 T3 ///
      $controls, robust cluster(school_id)
    eststo `var'
      // adding scalars:
      local noobs_scalar = round(e(N))
      local noobs "`noobs_scalar'"
        estadd local noobs `noobs', replace
      qui: sum `var' if T0==1
        estadd scalar cmean = abs(r(mean))

    qui: reg `var' T1 T2 T3 T1xtop_perf T2xtop_perf T3xtop_perf top_perf ///
      $controls, robust cluster(school_id)
    eststo `var'_x
      // adding scalars:
        forval i=1/3{
          estadd local rwolf`i' ``var'_rw`i'', replace
        }
      local noobs_scalar = round(e(N))
      local noobs "`noobs_scalar'"
        estadd local noobs `noobs', replace
      qui: sum `var' if T0==1
        estadd scalar cmean = abs(r(mean))
          forval x=1/3 {
            qui: test T`x'+T`x'xtop_perf=0
            estadd scalar T`x'_interacted = r(p), replace
          }
  }

  // Output Table 8

  esttab  parentmob1_z parentmob1_z_x formalparti_z formalparti_z_x  ///
          test_3_z test_3_z_x test_4_z test_4_z_x ///
    using "$Endline_outRaw/table8.csv", replace ///
    label b(3) se mtitle()  ///
    keep(T1 T2 T3 T1xtop_perf T2xtop_perf T3xtop_perf top_perf)			///
    s(rwolf1 rwolf2 rwolf3 cmean r2 noobs T1_interacted T2_interacted T3_interacted, ///
    label("T1 RW P-value" "T2 RW P-value" "T3 RW P-value" ///
    "Mean dep. variable (control)" "R-squared" "Observations" ///
    "T1 + T1xtop_perf = 0 (F p-val)" ///
    "T2 + T2xtop_perf = 0 (F p-val)" ///
    "T3 + T3xtop_perf = 0 (F p-val)")) ///
    title("Table 8: Heterogeneous effects by baseline school performance (dummy for top tercile)") starl(* 0.10 ** 0.05 *** 0.01) nonotes


  // Table 9 (Baseline education)

  // Calculate multiple hypotheses testing adjusted p-values
     * By row on the interaction term

    // Romano-Wolf Multiple Hypothesis Correction for Table 9

    forval i=1/3{
      rwolf $table89, ///
        indepvar(T`i'xlow_educ) ///
        controls(${controlsT`i'xlow_educ}) ///
        vce(cluster school_id) ///
        cluster(school_id) ///
        reps(`reps')

      foreach var in $table89{
        local `var'_scalar : di %-4.3f `e(rw_`var')'
        local `var'_rw`i' "[``var'_scalar']"
      }
    }

  // Regressions Table 9

  foreach var in $table89{
    qui: reg `var' T1 T2 T3 ///
      $controls, robust cluster(school_id)
    eststo `var'
      // adding scalars:
      local noobs_scalar = round(e(N))
      local noobs "`noobs_scalar'"
        estadd local noobs `noobs', replace
      qui: sum `var' if T0==1
        estadd scalar cmean = abs(r(mean))

    qui: reg `var' T1 T2 T3 T1xlow_educ T2xlow_educ T3xlow_educ low_educ ///
      $controls, robust cluster(school_id)
    eststo `var'_x
      // adding scalars:
        forval i=1/3{
          estadd local rwolf`i' ``var'_rw`i'', replace
        }
      local noobs_scalar = round(e(N))
      local noobs "`noobs_scalar'"
        estadd local noobs `noobs', replace
      qui: sum `var' if T0==1
        estadd scalar cmean = abs(r(mean))
          forval x=1/3 {
            qui: test T`x'+T`x'xlow_educ=0
            estadd scalar T`x'_interacted = r(p), replace
          }
      }

  // Output Table 9

  esttab  parentmob1_z parentmob1_z_x formalparti_z formalparti_z_x ///
          test_3_z test_3_z_x test_4_z test_4_z_x ///
    using "$Endline_outRaw/table9.csv", replace ///
    label b(3) se mtitle()  ///
    keep(T1 T2 T3 T1xlow_educ T2xlow_educ T3xlow_educ low_educ)			///
    s(rwolf1 rwolf2 rwolf3 cmean r2 noobs T1_interacted T2_interacted T3_interacted, ///
    label("T1 RW P-value" "T2 RW P-value" "T3 RW P-value" ///
    "Mean dep. variable (control)" "R-squared" "Observations" ///
    "T1 + T1xlow_educ = 0 (F p-val)" ///
    "T2 + T2xlow_educ = 0 (F p-val)" ///
    "T3 + T3xlow_educ = 0 (F p-val)")) ///
    title("Table 9: Heterogeneous effects by baseline education level (dummy for bottom tercile)") starl(* 0.10 ** 0.05 *** 0.01) nonotes


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* 3. Individual outcome variables

* Replication of main tables 3 - 7 for the individual dependent variables
* Appendix Tables A9 and A10

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  // Regressions

  local tables3567_individual $tables3567_individual

  foreach var in $tables3567_individual {
    qui: reg `var' T1 T2 T3 $controls, robust cluster(school_id)
    eststo `var'
      // adding scalars:
      local noobs_scalar = round(e(N))
      local noobs "`noobs_scalar'"
        estadd local noobs `noobs', replace
      qui: sum `var' if T0==1
        estadd scalar cmean = abs(r(mean))
  }


  * make tables

    esttab ${table3_individual} ${table5_individual} ${table6_individual} ${table7_individual} ///
     using "$Endline_outRaw/tableA9-A10.csv", replace ///
     label b(3) se mtitle() ///
     keep(T1 T2 T3) s(cmean r2 noobs, ///
     label("Mean dep. variable (control)" "R-squared" "Observations" ///
   )) title("Table A9-10: Treatment effects on individual dependent variables") ///
     starl(* 0.10 ** 0.05 *** 0.01) nonotes


  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  * 4. Replication of main regression Tables including block dummies

    * Tables A11 to A15

  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  // Calculate multiple hypotheses testing adjusted p-values
     * By row on each treatment

    // Controls for the rwolf estimation (with block dummies)

    // Romano-Wolf Multiple Hypothesis Correction

    forval x=11/15{
      forval i=1/3{
        rwolf ${tableA`x'}, ///
          indepvar(T`i') ///
          controls(${controlsT`i'_block}) ///
          vce(cluster school_id) ///
          cluster(school_id) ///
          reps(`reps') ///
          seed(20190723)

        foreach var in ${tableA`x'}{
          local `var'_scalar : di %-4.3f `e(rw_`var')'
          local `var'_rw`i' "[``var'_scalar']"
        }
      }
    }

    // Regressions

    local tables34567 $tables34567

    foreach var in $tables34567{
      qui: reg `var' T1 T2 T3 $controls_block, robust cluster(school_id)
      eststo `var'
        // adding scalars:
        forval i=1/3{
          estadd local rwolf`i' ``var'_rw`i'', replace
        }
      local noobs_scalar = round(e(N))
      local noobs "`noobs_scalar'"
        estadd local noobs `noobs', replace
      qui: sum `var' if T0==1
        estadd scalar cmean = abs(r(mean))
      qui: test T1+T2=T3
        estadd scalar complement = r(p), replace
      qui: test T1 T2 T3
        estadd scalar joint_all = r(p), replace
      qui: test T1=T2
        estadd scalar inf_col = r(p), replace
      qui: test T1=T3
        estadd scalar inf_duo = r(p), replace
      qui: test T2=T3
        estadd scalar col_duo = r(p), replace
    }

    // Output Tables A11 to A15

    forval x=11/15{
      esttab ${tableA`x'} ///
        using "$Endline_outRaw/tableA`x'.csv", replace ///
        label b(3) se mtitle() ///
        keep(T1 T2 T3) s(rwolf1 rwolf2 rwolf3 cmean r2 noobs inf_col inf_duo col_duo complement joint_all, ///
        label("T1 RW P-value" "T2 RW P-value" "T3 RW P-value" ///
          "Mean dep. variable (control)" "R-squared" "Observations" ///
          "T1 = T2 (F p-val)" ///
          "T1 = T3 (F p-val)" ///
          "T2 = T3 (F p-val)" ///
          "T1 + T2 = T3 (F p-val)" ///
          "T1 = T2 = T3 = 0 (F p-val)" ///
        )) title("Table A`x': ${titleA`x'}") ///
        starl(* 0.10 ** 0.05 *** 0.01) nonotes
    }




* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* 5. Heterogenous effect regression by gender of respondent

  * Tables A16

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// Regressions Table A16

foreach var in $tableA16{

  qui: reg `var' T1 T2 T3 T1xP_female T2xP_female T3xP_female ///
    $controls, robust cluster(school_id)
  eststo `var'
    // adding scalars:
    local noobs_scalar = round(e(N))
    local noobs "`noobs_scalar'"
      estadd local noobs `noobs', replace
    qui: sum `var' if T0==1
      estadd scalar cmean = abs(r(mean))
        forval x=1/3 {
          qui: test T`x'+T`x'xP_female=0
          estadd scalar T`x'_interacted = r(p), replace
        }
}

// Output Table A16

esttab  $tableA16 ///
  using "$Endline_outRaw/tableA16.csv", replace ///
  label b(3) se mtitle()  ///
  keep(T1 T2 T3 T1xP_female T2xP_female T3xP_female P_female)			///
  s(cmean r2 noobs T1_interacted T2_interacted T3_interacted, ///
  label( ///
  "Mean dep. variable (control)" "R-squared" "Observations" ///
  "T1 + T1xFemale = 0 (F p-val)" ///
  "T2 + T2xFemale = 0 (F p-val)" ///
  "T3 + T3xFemale = 0 (F p-val)")) ///
  title("Table A16: Heterogeneous effects on parent's mobilization at home by gender of respondent") starl(* 0.10 ** 0.05 *** 0.01) nonotes



    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  * 6. Replication of main regression Table 3 no parents in formal participation

    * Table A17

  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  // Romano-Wolf Multiple Hypothesis Correction for Table A17


    forval i=1/3{
      rwolf $tableA17, ///
        indepvar(T`i') ///
        controls(${controlsT`i'}) ///
        vce(cluster school_id) ///
        cluster(school_id) ///
        reps(`reps') ///
        seed(20190723)

      foreach var in $tableA17{
        local `var'_scalar : di %-4.3f `e(rw_`var')'
        local `var'_rw`i' "[``var'_scalar']"
      }
    }

  // Regressions Table A17

  foreach var in $tableA17{
    qui: reg `var' T1 T2 T3 $controls, robust cluster(school_id)
    eststo `var'
      // adding scalars:
      forval i=1/3{
        estadd local rwolf`i' ``var'_rw`i'', replace
      }
    local noobs_scalar = round(e(N))
    local noobs "`noobs_scalar'"
      estadd local noobs `noobs', replace
    qui: sum `var' if T0==1
      estadd scalar cmean = r(mean)
    qui: test T1+T2=T3
      estadd scalar complement = r(p), replace
    qui: test T1 T2 T3
      estadd scalar joint_all = r(p), replace
    qui: test T1=T2
      estadd scalar inf_col = r(p), replace
    qui: test T1=T3
      estadd scalar inf_duo = r(p), replace
    qui: test T2=T3
      estadd scalar col_duo = r(p), replace
  }

  // Table titles

  global titleA17 "Treatment effects on parents’ mobilization (adjusted formal participation)"

  // Output Table A17

    esttab $tableA17 ///
      using "$Endline_outRaw/tableA17.csv", replace ///
      label b(3) se mtitle() ///
      keep(T1 T2 T3) s(rwolf1 rwolf2 rwolf3 cmean r2 noobs inf_col inf_duo col_duo complement joint_all, ///
      label("T1 RW P-value" "T2 RW P-value" "T3 RW P-value" ///
        "Mean dep. variable (control)" "R-squared" "Observations" ///
        "T1 = T2 (F p-val)" ///
        "T1 = T3 (F p-val)" ///
        "T2 = T3 (F p-val)" ///
        "T1 + T2 = T3 (F p-val)" ///
        "T1 = T2 = T3 = 0 (F p-val)" ///
      )) title("Table A17: $titleA17") ///
      starl(* 0.10 ** 0.05 *** 0.01) nonotes



* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

****** End of dofile
